package com.dys.controller;

import com.dys.pojo.Role;
import com.dys.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @className: com.dys.controller.RoleController
 * @description: 角色管理
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/11/15 20:31
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @GetMapping(value = "/selectRoles")
    // @ResponseBody
    public String selectRoles(Role role, Model model) throws Exception {
        List<Role> list = roleService.listRoleInfoByRole(role);
        model.addAttribute("list", list);
        return "role/role";
    }

    /**
     * 处理页面跳转
     */
    @RequestMapping(value = "/roleDispatch")
    public String handlePageDispatch(Integer roleId, Model model) throws Exception {
        if (roleId != null) {
            System.out.println("角色编号：" + roleId);
            Role role = roleService.getRoleInfoByRoleId(roleId);
            model.addAttribute("role", role);
        }
        return "role/updateRole";
    }

    @RequestMapping(value = "/saveRoleInfo")
    public String saveRoleInfo(Role role) throws Exception {
        if (role.getRoleId() != null && role.getRoleId() > 0) {
            // 更新角色信息
            roleService.updateRoleInfo(role);
        } else {
            roleService.addRoleInfo(role);
        }
        return "redirect:/role/selectRoles";
    }

    @RequestMapping(value = "deleteRoleByRoleId")
    public String deleteRoleByRoleId(Integer roleId) throws Exception {
        roleService.deleteRoleInfo(roleId);
        return "redirect:/role/selectRoles";
    }
}
